package com.luke.exercise.aop;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor{

    
    @SuppressWarnings("unchecked")
    public <T>T getProxy(Class<T> clazz) {
        return (T) Enhancer.create(clazz, this);
    }
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
            throws Throwable {
        before();
        Object result = proxy.invokeSuper(obj, args);
        after();
        return result;
    }
    
    private void before() {
        System.out.println("before");
    }
    
    private void after() {
        System.out.println("after");
    }
    
    public static void main(String[] args) {
        CglibProxy proxy = new CglibProxy();
        Hello1 hello = proxy.getProxy(HelloImpl.class);
        hello.say("luke");
    }

}
